package com.sxt.bus.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.sxt.bus.BUSConstast.BUSConstast;
import com.sxt.bus.domain.Goods;
import com.sxt.bus.domain.Provider;
import com.sxt.bus.service.GoodsService;
import com.sxt.bus.service.ProviderService;
import com.sxt.bus.vo.GoodsVo;
import com.sxt.bus.vo.ProviderVo;
import com.sxt.sys.SYSConstast.SYSConstast;
import com.sxt.sys.utils.ResultObj;
import com.sxt.bus.utils.DataGridView;
import com.sxt.bus.utils.WebUtils;

@RestController
@RequestMapping("goods")
public class GoodsController {
	@Value("${upload.uploadPath}")
	private String uploadpath;
	
	@Autowired
	private GoodsService goodsService;
	
	@Autowired
	private ProviderService providerService;
	
	
	@RequestMapping("loadAllGoods")
	public DataGridView loadAllGoods(GoodsVo goodsVo) {
		
		return goodsService.queryAllGoods(goodsVo);
	}
	
	@RequestMapping("deleteGoods")
	public ResultObj deleteGoods(GoodsVo goodsVo) {
		Integer code=1;
		String msg="";
		try {
			
					String goodsimg = goodsVo.getGoodsimg();
								
					String relpath = uploadpath+ goodsimg;
					File f = new File(relpath);
					if(f.exists()){
						f.delete();
					}
			Integer info = goodsService.deleteGoods(goodsVo.getId());
			code = info;
			msg = info > 0 ?SYSConstast.DELETE_LOGININFOR_TRUE:SYSConstast.DELETE_LOGININFOR_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = SYSConstast.DELETE_LOGININFOR_FALSE;
		}
		return new ResultObj(code , msg);
	}
	
	@RequestMapping("updateGoods")
	public ResultObj updateGoods(GoodsVo goodsVo) {
		Integer code=1;
		String msg="";
		try {
			Integer info = goodsService.updateGoods(goodsVo);
			code = info;
			msg = info > 0 ?BUSConstast.UPDATE_LOGININFOR_TRUE:BUSConstast.UPDATE_LOGININFOR_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = BUSConstast.UPDATE_LOGININFOR_FALSE;
		}
		return new ResultObj(code , msg);
	}
	
	
	@RequestMapping("addGoods")
	public ResultObj addGoods(GoodsVo goodsVo) {
		Integer code=1;
		String msg="";
		try {
			Integer info = goodsService.addGoods(goodsVo);
			code = info;
			msg = info > 0 ?BUSConstast.ADD_LOGININFOR_TRUE:BUSConstast.ADD_LOGININFOR_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = BUSConstast.ADD_LOGININFOR_FALSE;
		}
		return new ResultObj(code , msg);
	}
	
	
	
	
	@RequestMapping("loadAllProvider")
	public List<Provider> loadAllProvider() {
		return providerService.loadAllgoods();
		
	}
	@RequestMapping("loadGoodsByProviderId")
	public List<Goods> loadGoodsByProviderId(Integer providerid){
		return goodsService.loadGoodsByProviderId(providerid);
	}
}
